package com.ckd.flyingtrip.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.adapter.GridViewAdapter;
import com.ckd.flyingtrip.utils.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;
/*
* 故障上报
* */
public class MyfaultActivity extends BaseActivity {
    @BindView(R.id.cd_title_img)
    ImageView cdTitleImg;
    @BindView(R.id.cd_close)
    TextView cdClose;
    @BindView(R.id.cd_title_btn)
    TextView cdTitleBtn;
    @BindView(R.id.edit_info)
    EditText editInfo;
    @BindView(R.id.gridview)
    GridView gridview;
    @BindView(R.id.btn_res)
    TextView btnRes;
    private GridViewAdapter gridViewAdapter;
    private int num = 6;
    private   List<String> path = new ArrayList<>();
    private   List<String> pathUrl = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fault);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        Tools.title_info(MyfaultActivity.this,"故障上报");
        gridViewAdapter = new GridViewAdapter(MyfaultActivity.this,pathUrl);
        gridview.setAdapter(gridViewAdapter);

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==parent.getChildCount()-1){
                    MultiImageSelector.create(MyfaultActivity.this)
                            .showCamera(true) // 是否显示相机. 默认为显示
                            .count(num) // 最大选择图片数量, 默认为9. 只有在选择模式为多选时有效
                            .multi() // 多选模式, 默认模式;
                            .start(MyfaultActivity.this, 1);
                }
            }
        });
    }

    @OnClick(R.id.btn_res)
    public void onClick() {

    }

    public void deleImg(int in){
        if(pathUrl!=null){
            pathUrl.remove(in);
            num++;
            gridViewAdapter.notifyDataSetChanged();
        }
    }




    //回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1){
            if(resultCode == RESULT_OK){
                // 获取返回的图片列表
                 path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                System.out.println("图片路径"+path);
                for (int img_i=0;img_i<path.size();img_i++) {
                    final String pathImg = path.get(img_i);
                    pathUrl.add(pathImg);
                    num--;
                }
                gridViewAdapter.notifyDataSetChanged();
            }
        }
    }
}
